﻿using CarPCBATest.Procedure;
using Common;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Shapes;

namespace CarPCBATest.ViewModel
{
    internal class SettingViewModel : NotifyBase
    {
        private AllPcbConfig allPcbConfig;

        /// <summary>
        /// 所有PCB配置
        /// </summary>
        public AllPcbConfig AllPcbConfig
        {
            get { return allPcbConfig; }
            set
            {
                allPcbConfig = value;
                this.DoNotify();
            }
        }

        private PcbConfig selectedPcbConfig;

        /// <summary>
        /// 当前选中PCB配置
        /// </summary>
        public PcbConfig SelectedPcbConfig
        {
            get { return selectedPcbConfig; }
            set
            {
                if (value == null)
                {
                    selectedPcbConfig = null;
                }
                selectedPcbConfig = value;
                this.DoNotify();
            }
        }

        private SubBurnConfig selectedSubBurnConfig;

        public SubBurnConfig SelectedSubBurnConfig
        {
            get { return selectedSubBurnConfig; }
            set
            {
                selectedSubBurnConfig = value;

                this.DoNotify();
            }
        }

        private LedCurrentConfig _selectedLedCurrentConfig;

        public LedCurrentConfig SelectedLedCurrentConfig
        {
            get { return _selectedLedCurrentConfig; }
            set
            {
                _selectedLedCurrentConfig = value;
                this.DoNotify();
            }
        }

        public SettingViewModel()
        {
            AllPcbConfig = PcbConfigHelper.Load();
            SelectedPcbConfig = AllPcbConfig.ListConfig.Where(t => t.No == AllPcbConfig.UsingNo).ToList()[0];
            //SelectedPcbConfig = PcbConfigHelper.GetTestConfig();
            SetVisionProjPathCommand = new CommandBase(SetVisionProjPath);
            SetBrunFilePathCommand = new CommandBase(SetBrunFile);

            DelProjCommand = new CommandBase(DelProj);
            SaveProjCommand = new CommandBase(SaveProj);
            CopyAddProjCommand = new CommandBase(CopyAddProj);
            DelBurnCfgCommand = new CommandBase(DelBurnCfg);

            CopyAddBurnCfgCommand = new CommandBase(CopyAddBurnCfg);

            DelFctTestCfgCommand = new CommandBase(DelFctTestCfg);
            CopyAddFctTestCfgCommand = new CommandBase(CopyAddFctTestCfg);
            UnloadedCommand = new CommandBase(Unloaded);
            LoadedCommand = new CommandBase(Loaded);
        }

        private void Loaded(object obj)
        {
            AllPcbConfig = PcbConfigHelper.Load();
            SelectedPcbConfig = AllPcbConfig.ListConfig.Where(t => t.No == AllPcbConfig.UsingNo).ToList()[0];

        }

        private static readonly object locker = new object();

        private void Unloaded(object obj)
        {
            try
            {
                Stopwatch sw = Stopwatch.StartNew();
                SaveProj(null);
                //Console.WriteLine(sw.ElapsedMilliseconds);

                //Task.Run(() =>
                //{
                //    try
                //    {
                //        lock (locker)
                //        {
                //            // 设置供电
                //            if (SelectedPcbConfig != null)
                //            {
                //                foreach (var power in SelectedPcbConfig.ListPowerConfig)
                //                {
                //                    IT6302A.SetChannelOutput(power.ChannelIndex, power.IsActive);
                //                    IT6302A.SetVoltage(power.ChannelIndex, power.Voltage);
                //                    IT6302A.SetCurrent(power.ChannelIndex, power.Current);
                //                }
                //            }
                //        }
                //    }
                //    catch (Exception ex)
                //    {
                //        Mylog.Error(ex, "电压表配置异常");
                //    }
                //});
                //Console.WriteLine(sw.ElapsedMilliseconds);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void CopyAddFctTestCfg(object obj)
        {
            try
            {
                SelectedPcbConfig.ListLedCurrentConfig.Add(Tools.DeepClone<LedCurrentConfig>(SelectedLedCurrentConfig));
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void DelFctTestCfg(object obj)
        {
            try
            {
                if (SelectedPcbConfig.ListLedCurrentConfig.Count <= 1)
                {
                    ShowMessage("至少保留一个测试配置", "警告");
                    return;
                }
                SelectedPcbConfig.ListLedCurrentConfig.Remove(SelectedLedCurrentConfig);
                if (SelectedPcbConfig.ListLedCurrentConfig.Count > 0)
                {
                    SelectedLedCurrentConfig = SelectedPcbConfig.ListLedCurrentConfig.Last();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void CopyAddBurnCfg(object obj)
        {
            try
            {
                SelectedPcbConfig.BurnConfig.SubBurnConfigs.Add(Tools.DeepClone<SubBurnConfig>(SelectedSubBurnConfig));
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void DelBurnCfg(object obj)
        {
            try
            {
                if (SelectedPcbConfig.BurnConfig.SubBurnConfigs.Count <= 0)
                {
                    ShowMessage("至少保留一个烧录配置", "警告");
                    return;
                }
                SelectedPcbConfig.BurnConfig.SubBurnConfigs.Remove(SelectedSubBurnConfig);
                if (SelectedPcbConfig.BurnConfig.SubBurnConfigs.Count > 0)
                {
                    SelectedSubBurnConfig = SelectedPcbConfig.BurnConfig.SubBurnConfigs.Last();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void CopyAddProj(object obj)
        {
            try
            {
                PcbConfig pcbConfig = Tools.DeepClone<PcbConfig>(SelectedPcbConfig);
                pcbConfig.No = AllPcbConfig.GetNewProjNo();
                AllPcbConfig.ListConfig.Add(pcbConfig);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void ShowMessage(string msg, string msgType = "错误")
        {
            var mwin = Application.Current.MainWindow as MetroWindow;

            mwin.ShowMessageAsync(msgType, msg);
        }

        private void SaveProj(object obj)
        {
            try
            {
                if (SelectedPcbConfig is not null)
                {
                    AllPcbConfig.UsingNo = SelectedPcbConfig.No;
                }
                PcbConfigHelper.Save(AllPcbConfig);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void DelProj(object obj)
        {
            try
            {
                if (AllPcbConfig.ListConfig.Count <= 1)
                {
                    ShowMessage("至少保留一个方案", "警告");
                    return;
                }
                AllPcbConfig.ListConfig.Remove(SelectedPcbConfig);
                if (AllPcbConfig.ListConfig.Count > 0)
                {
                    SelectedPcbConfig = AllPcbConfig.ListConfig.Last();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void SetBrunFile(object obj)
        {
            // 创建 OpenFileDialog 对象
            OpenFileDialog openFileDialog = new OpenFileDialog();

            // 设置对话框的属性
            openFileDialog.Title = "选择文件"; // 对话框标题
            openFileDialog.Filter = "文本文件 (*.csv)|*.csv|所有文件 (*.*)|*.*"; // 文件过滤器，只显示指定类型的文件
            openFileDialog.RestoreDirectory = false;
            openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(SelectedSubBurnConfig.BrunFilePath);
            // 打开对话框并获取用户选择的文件
            bool? result = openFileDialog.ShowDialog();

            // 处理用户选择的文件
            if (result == true)
            {
                string selectedFilePath = openFileDialog.FileName;
                SelectedSubBurnConfig.BrunFilePath = selectedFilePath;
                // 在这里可以处理选择的文件路径
            }
        }

        private void SetVisionProjPath(object obj)
        {
            // 创建 OpenFileDialog 对象
            OpenFileDialog openFileDialog = new OpenFileDialog();

            // 设置对话框的属性
            openFileDialog.Title = "选择文件"; // 对话框标题
            openFileDialog.Filter = "文本文件 (*.sol)|*.sol|所有文件 (*.*)|*.*"; // 文件过滤器，只显示指定类型的文件
            openFileDialog.RestoreDirectory = false;
            openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(SelectedPcbConfig.VisionProjPath);
            // 打开对话框并获取用户选择的文件
            bool? result = openFileDialog.ShowDialog();

            // 处理用户选择的文件
            if (result == true)
            {
                string selectedFilePath = openFileDialog.FileName;
                SelectedPcbConfig.VisionProjPath = selectedFilePath;
                // 在这里可以处理选择的文件路径
            }
        }

        public CommandBase SetVisionProjPathCommand { get; set; }
        public CommandBase SetBrunFilePathCommand { get; set; }
        public CommandBase DelProjCommand { get; set; }
        public CommandBase SaveProjCommand { get; set; }
        public CommandBase CopyAddProjCommand { get; set; }
        public CommandBase DelBurnCfgCommand { get; set; }

        public CommandBase CopyAddBurnCfgCommand { get; set; }

        public CommandBase DelFctTestCfgCommand { get; set; }
        public CommandBase CopyAddFctTestCfgCommand { get; set; }
        public CommandBase UnloadedCommand { get; set; }
        public CommandBase LoadedCommand { get; set; }
    }
}